package codetemplate.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class SQLTest {
	public static void main(String[] args) {
		
		
		Configuration configuration = new Configuration();

		configuration.setProperty("hibernate.connection.driver_class" ,   
	            "com.mysql.jdbc.Driver")  
	            .setProperty("hibernate.connection.url" , "jdbc:mysql://localhost/collect")  
	            .setProperty("hibernate.connection.username" , "root")  
	            .setProperty("hibernate.connection.password" , "root")  
	            .setProperty("hibernate.c3p0.max_size" , "5")  
	            .setProperty("hibernate.c3p0.min_size" , "1")  
	            .setProperty("hibernate.c3p0.timeout" , "5000")  
	            .setProperty("hibernate.c3p0.max_statements" , "100")  
	            .setProperty("hibernate.c3p0.idle_test_period" , "3000")  
	            .setProperty("hibernate.c3p0.acquire_increment" , "2")  
	            .setProperty("hibernate.c3p0.validate" , "true")  
	            .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")  
	            .setProperty("hibernate.hbm2ddl.auto" , "create")  
				.setProperty("hibernate.show_sql","true");
		
		configuration.addClass(CrawlURL.class);
		
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CrawlURL.class); 
		detachedCriteria.add(Restrictions.eq("url_name", "人民日报"));
		//createAlias("employees", "e").
		Session session = null;
		try {
			SessionFactory sessionFactory = configuration.buildSessionFactory();
			session = sessionFactory.openSession();
			
			List l = detachedCriteria.getExecutableCriteria(session ).list();
			
//			Query q = session.createSQLQuery("select * from crawl_url");//.addEntity(CrawlURL.class);
//			List l = q.list();
			
			System.out.println( l.size() );
			for (int i = 0; i < l.size(); i++) {
//				System.out.println(l);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if( session != null ) session.close();
		}
		
		
	}
}
